﻿using MyFramework.Core.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace MyFramework.Web.Api
{
    /// <summary>
    /// 插件相关路径
    /// </summary>
    public class MyPluginPathProvider
    {

        /// <summary>
        /// 静态构造
        /// </summary>
        static MyPluginPathProvider()
        {
            string temp = ConfigurationHelper.Configuration["MyPlugin:Paths:Temp"];
            string plugins = ConfigurationHelper.Configuration["MyPlugin:Paths:Plugins"];
            string sourceCode = ConfigurationHelper.Configuration["MyPlugin:Paths:SourceCode"];

            if (string.IsNullOrEmpty(temp))
            {
                Temp = Path.Combine(Directory.GetCurrentDirectory(), "Temp");
            }
            else
            {
                Temp = Path.Combine(Directory.GetCurrentDirectory(), temp);
            }

            if (string.IsNullOrEmpty(plugins))
            {
                Plugins = Path.Combine(Directory.GetCurrentDirectory(), "Plugins");
            }
            else
            {
                Plugins = Path.Combine(Directory.GetCurrentDirectory(), plugins);
            }

            if (string.IsNullOrEmpty(sourceCode))
            {
                SourceCode = Path.Combine(Directory.GetCurrentDirectory(), "SourceCode");
            }
            else
            {
                SourceCode = Path.Combine(Directory.GetCurrentDirectory(), sourceCode);
            }

            if (!Directory.Exists(Temp))
                Directory.CreateDirectory(Temp);
            if (!Directory.Exists(Plugins))
                Directory.CreateDirectory(Plugins);
            if (!Directory.Exists(SourceCode))
                Directory.CreateDirectory(SourceCode);
        }
        /// <summary>
        /// 临时目录
        /// </summary>
        public static string Temp { get; set; }
        /// <summary>
        /// 插件路径(dll)
        /// </summary>
        public static string Plugins { get; set; }
        /// <summary>
        /// 插件路径(cs)
        /// </summary>
        public static string SourceCode { get; set; }
        /// <summary>
        /// 临时插件上传目录路径
        /// eg: F:\Com\me\Repos\Remember.Core\src\Presentation\WebApi\App_Data\TempPluginUpload
        /// </summary>
        /// <returns></returns>
        public static string TempPluginUploadDir()
        {
            string tempPluginUploadDir = Path.Combine(Directory.GetCurrentDirectory(), "App_Data", "TempPluginUpload");
            return tempPluginUploadDir;
        }

        /// <summary>
        /// 获取 Plugins 的路径
        /// </summary>
        /// <returns></returns>
        public static string PluginsRootPath()
        {
            string pluginRootPath = Path.Combine(Directory.GetCurrentDirectory(), "Plugins");

            return pluginRootPath;
        }

        /// <summary>
        /// 获取目标插件文件夹名
        /// </summary>
        /// <param name="pluginDir">目标插件完整目录路径</param>
        /// <returns></returns>
        public static string GetPluginFolderNameByDir(string pluginDir)
        {
            string pluginRootPath = Plugins;
            string pluginFolderName = pluginDir.Replace(pluginRootPath + Path.DirectorySeparatorChar, "");

            return pluginFolderName;
        }

        /// <summary>
        /// 所有插件的完整目录路径
        /// </summary>
        /// <returns></returns>
        public static IList<string> AllPluginDir()
        {
            string pluginRootPath = Plugins;
            string[] pluginDirs = Directory.GetDirectories(pluginRootPath, "*");

            return pluginDirs;
        }

        /// <summary>
        /// 所有插件的文件夹名
        /// </summary>
        /// <returns></returns>
        public static IList<string> AllPluginFolderName()
        {
            IList<string> pluginFolderNames = new List<string>();
            IList<string> pluginDirs = AllPluginDir();
            foreach (var dir in pluginDirs)
            {
                string pluginFolderName = GetPluginFolderNameByDir(dir);
                pluginFolderNames.Add(pluginFolderName);
            }

            return pluginFolderNames;
        }

    }
}
